package cn.uhua.erp.pms.common.model;

import jakarta.persistence.*;
import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

@Data
@Entity
@Table(name = "authorities", 
       uniqueConstraints = @UniqueConstraint(name = "ix_auth_username", columnNames = {"username", "authority"}))
@IdClass(AuthorityId.class) // 指定复合主键类
public class Authority implements Serializable {

    @Id
    @ManyToOne
    @JoinColumn(name = "username", referencedColumnName = "username", nullable = false)
    private User user;

    @Id
    @Column(name = "authority", length = 50, nullable = false)
    private String authority;

    // 重写equals和hashCode方法，基于复合主键
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Authority authority1 = (Authority) o;
        return Objects.equals(user.getUsername(), authority1.user.getUsername()) &&
                Objects.equals(authority, authority1.authority);
    }

    @Override
    public int hashCode() {
        return Objects.hash(user.getUsername(), authority);
    }
}
